package com.cms.recursoshumanos.activity.pantallas_principal;

import com.cms.recursoshumanos.activity.main.MainRecursosHumanosActivity;
import com.cms.recursoshumanos.activity.menu_comida.ListaTipoMenuActivity;
import com.cms.recursoshumanos.activity.user.poliza.DetailPolizaActivity;
import com.cms.recursoshumanos.cache.UserInfoCache;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.handler.DetailPolizaActivityHandler;
import com.cms.recursoshumanos.handler.ListaTipoMenuActivityHandler;
import com.cms.recursoshumanos.object.Usuario;
import com.cms.recursoshumanos.utilities.Tool;
import com.example.frontendrecursoshumanos.R;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;

public class PrincipalBeneficiosMenuActivity extends MainRecursosHumanosActivity 
{

	protected static MenuComedorAsync menuComedorAsyncTask;
	protected static DetailPolizaAsync asynDetailPolizaTask;
	private Usuario elUsuario;
	private ProgressDialog dialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu_beneficios_principal);
		elUsuario = UserInfoCache.getInstance().getUsuario();
	}
	@Override
	protected void onResume() 
	{
		super.onResume();
		if (elUsuario == null)
		{
			elUsuario = UserInfoCache.getInstance().getUsuario();
		}
	}
	
	
	public void showMenuComedor (View v)
    {
		findInfoTipoMenu();
	}
	public void showListaOcio(View v)
    {
		goMenuOcio();
	}
	public void showCatalogo(View v)
	{
		goCatalogoPrincipal();
	}

	public void showPoliza(View v)
    {
		findDetallePoliza();
	}

	public void findDetallePoliza()
	{
		asynDetailPolizaTask = new DetailPolizaAsync(elUsuario);
		asynDetailPolizaTask.execute();
	}
	protected void findInfoTipoMenu()
	{
		menuComedorAsyncTask = new MenuComedorAsync(elUsuario);
		menuComedorAsyncTask.execute();
	}
	

	
	public void goDetailPoliza()
	{
		Intent intent = new Intent(PrincipalBeneficiosMenuActivity.this, DetailPolizaActivity.class);
		startActivity(intent);
	}
	public void goTipoMenu()
	{
		Intent intent = new Intent(PrincipalBeneficiosMenuActivity.this, ListaTipoMenuActivity.class);
		startActivity(intent);
	}
	public void goCatalogoPrincipal()
	{
		Intent intent = new Intent(PrincipalBeneficiosMenuActivity.this, PrincipalTiendaMenuActivity.class);
		startActivity(intent);
	}
	
	public void goMenuOcio()
	{
		Intent intent = new Intent(PrincipalBeneficiosMenuActivity.this,  PrincipalOcioMenuActivity.class);
		startActivity(intent);
	}
	
	private class MenuComedorAsync extends AsyncTask<String, Void, String>
	{
		
		private Usuario elUsuario;
		
		public MenuComedorAsync(Usuario elUsuario)
		{
			this.elUsuario = elUsuario;
		}

		@SuppressWarnings("deprecation")
		@Override
		protected void onPreExecute()
		{
			showDialog(0);
			dialog = ProgressDialog.show(PrincipalBeneficiosMenuActivity.this, "Espere","Cargando...",true );
		}
	
		@Override
		protected String doInBackground(String... arg0) 
		{
			ListaTipoMenuActivityHandler handler = new ListaTipoMenuActivityHandler();
			String result = handler.getListadoTipoMenu(this.elUsuario.getNombre_Usuario(), 
														this.elUsuario.getPassword());
			return result;
		}
		
		@Override
		public void onPostExecute(String  result)
		{
			
			String str_result = result;
			
			if (str_result.equals(Constant.CODE_OK))
			{ 
				goTipoMenu();
			}
			else 
			{
				Tool.getInstance().showMessage( str_result);
			}
			dialog.cancel();
			this.cancel(true);
			
		}
		
	}
	
	
	private class DetailPolizaAsync extends AsyncTask<String, Void, String>
	{
		
		private Usuario elUsuario;

		public  DetailPolizaAsync(Usuario elUsuario)
	    {
			this.elUsuario = elUsuario;
	    }
		
		@SuppressWarnings("deprecation")
		@Override
		protected void onPreExecute()
		{
			showDialog(0);
			dialog = ProgressDialog.show(PrincipalBeneficiosMenuActivity.this, "Espere","Cargando...",true );
		}
	
		@Override
		protected String doInBackground(String... arg0) 
		{
			DetailPolizaActivityHandler handler = new DetailPolizaActivityHandler();
			String result = handler.getDetailPoliza(this.elUsuario.getNombre_Usuario(),
														this.elUsuario.getPassword());
			return result;
		}
		
		@Override
		public void onPostExecute(String  result)
		{
			
			String str_result = result;
			
			if (str_result.equals(Constant.CODE_OK))
			{ 
				Tool.getInstance().saveUserInfoShared(7,UserInfoCache.getInstance().getPoliza());
				goDetailPoliza();
			}
			else 
			{
				Tool.getInstance().showMessage( str_result);
			}
			dialog.cancel();
			this.cancel(true);
		}
		
	}
	
	
	
	
	
	
}
